home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / TPTUTR0F.ZIP / PASCAL1.TXT < prev    next >
Text File  |  1995-12-03  |  11KB  |  263 lines

  1.                         Pascal Tutorial: Learning Pascal
  2.                              by Glenn Grotzinger
  3.                            Part 1 -- Starting Out.
  4.              all parts copyright (c) 1995-1996 by Glenn Grotzinger.
  5.  
  6.         I am writing this tutorial as a means for people to start out and
  7. learn a great deal about Pascal.  I write this part with the understanding
  8. that you have looked at the compiler, and have figured out how to use it to
  9. enter a program, how to compile and run a program within the IDE, save a
  10. source code file, or compile at the DOS prompt, or in the IDE to create an
  11. EXE file; but do not have any understanding of Pascal programming as a whole.
  12. Any subsequent parts will be written assuming that you have read prior parts, and fully understand all the examples.  Please work through
  13. the examples, entering them in yourself, to get used to programming in Pascal.
  14. I also recommend that you print all parts out, so you may have reference
  15. for any future parts that we see.
  16.         As I am writing each part as we go, for right now, I will try
  17. to have each part written and posted on a weekly basis.  
  18.  
  19. About this part
  20. ===============
  21. This will be a lengthy part, since we are trying to introduce enough material
  22. to get to the point of writing a simple program.
  23.  
  24. The basics
  25. ==========
  26.         My intent in this section is to familiarize you with the proper
  27. starting structure to remember for to get ready to write a program.  Let's
  28. look at the short example below. (I numbered the lines with {}'s for purposes
  29. of this explanation.)
  30.  
  31. {1} program tutorial1; uses crt;
  32. {2}   var
  33. {3}     world_stmt: string;
  34. {4}   begin
  35. {5}     world_stmt := 'Hello world!';
  36. {6}     writeln(world_stmt);
  37. {7}   end.
  38.  
  39. This is a relatively simple program for starting out.  Let us run through
  40. what each line has.
  41.  
  42. {1} this is the programID, and the uses clause.
  43.     program <identifer>; uses <library>;
  44.     is the syntax.  <identifier> may be anything that we may decide to
  45.     call the program. <library> is a specification of additional libraries,
  46.     or *units* of commands that we may want to use.
  47.  
  48.     crt is the name of one library we will use a lot in our programming.
  49.     The commands that can be accessed in this library will be detailed later
  50.     on in the tutorial.  If we want to use any of the commands in a library,
  51.     we must tell the compiler to use the library, and the uses statement
  52.     does this.  I will specify if any commands we use will come out of a
  53.     unit.
  54.  
  55. {2} var is a signal we use at the beginning of a program block to tell the
  56.     compiler that we want to start defining variables.
  57.  
  58. {3} This is a definition of a string, or sequence of text.  Variable defs.
  59.     will be covered later in this part.  We are defining a variable named
  60.     world_stmt to be a string.
  61.  
  62. {4} begin says we want to begin a block of code.
  63.  
  64. {5} & {6} are some program commands.  We will explain them later.
  65.  
  66. {7} end; ends a program block.
  67.  
  68. A few basic commands
  69. ====================
  70. I will now discuss variable definitions.
  71.     string: a section of text. "Hello world!" would be a string.
  72.     integer: a number which does not have a decimal part. 12 is an integer.
  73.     char: one part of a string.  "G" is a character, while "GG" is not.
  74.     real: a number which has a decimal part.  3.25 would be a real.
  75.  
  76. Comments.
  77.     If you wish to make some text as a remark to what something does,
  78.     use the { key or (* to start and the } or *) to end it.
  79.  
  80. The assign.
  81.     We use the := to assign a value to a variable.
  82.     Examples of that would be such as:
  83.        world_stmt := 'Hello world.';   { a definition of a string.  The
  84.                                         ' s must be there on each side }
  85.        choice_char := 'a';             { a definition of a character The
  86.                                         ' s must be there on each side }
  87.        money := 3.25;                  { a definition of a real }
  88.        coins := 10;                    { a definition of an integer }
  89.                                                                   
  90. Arithmetic computations.
  91.     We often have to do arithmetic to program and solve a problem.  I will
  92.     illustrate addition, subtraction, multiplication, and division.
  93.  
  94.        sum := 3 + 2;   { we're telling the computer to add 3 and 2 and then
  95.                          place 5 in an integer called sum.  Assignments can
  96.                          also work with this way }
  97.  
  98.        sub := 10 - 7;  { we're telling the computer to subtract 7 from 10
  99.                          and then place 3 in an integer called sub. }
  100.  
  101.        mult := 3 * 2;  { we're telling the computer to multiply 3 by 2. }
  102.  
  103.        divisn := 10 / 2; {dividing 10 by 2 }
  104.  
  105.      Any of these can be combined in one statement, with the order of
  106.      operations being /, *, -, +.  ('s may be used to force one group of
  107.      numbers.  For example:
  108.  
  109.        answer := 3 + (2 + 6) * 4;
  110.  
  111.      Rules:  We must use the following idea to determine whether things are
  112.      OK to do for arithmetic.
  113.         1) If we perform an arithmetic operation with anything with a real
  114.            in it, the receiving variable in the assign must be defined as
  115.            a real.
  116.         2) If we perform a division with two integers that have a chance
  117.            of dividing to become a real, we must use a real for a receiving
  118.            variable.  12 / 7 would be an example and be 1.71 (rounded to 2
  119.            decimal places).
  120.      Note: dividing and getting a real or using a real in the other stuff
  121.      will result in answers such as 3.232133412E+02.  I will cover later
  122.      the way to make that look readable and normal.
  123.  
  124. We will now look at an example of some of the stuff above.
  125.  
  126. program tutorial2;
  127.   var
  128.     first_number, second_number: integer;
  129.     result: integer;
  130.   begin
  131.     first_number := 3;
  132.     {assign first number the value of 3}
  133.     second_number := first_number * 2;
  134.     {assign 2nd number-multiply first number by 2}
  135.     first_number := second_number - 5;
  136.     {assign 1st number-old value of 1st number - 5}
  137.     result := first_number + second_number;
  138.     {assign result to be 1st number + 2nd number}
  139.   end.
  140.  
  141. A question to understand what is going on.  Answer this one, and you
  142. understand everything up to this point.  What is the value of each and
  143. every one of the integer variables as listed in the tutorial2 after all
  144. the statements execute? (Answer will appear in the next part).
  145.  
  146. DIV and MOD
  147.      These are special operators.  Div places the whole number of a division
  148.      in the receiving variable, and MOD places the remainder.  For example:
  149.  
  150.      whole := 12 div 7;  {whole becomes 1}
  151.      remainder := 12 mod 7; {remainder becomes 5}
  152.  
  153.      7 goes into 12 one time with a remainder of five.
  154.  
  155. reading and writing information.
  156.      We will stick to use of the keyboard for reading information, and
  157.      the monitor for writing information right now.  Remember for any
  158.      variable, we must not define it to be the name of a command, when we
  159.      do this.
  160.  
  161.      read(a_number);
  162.  
  163.      This command stops the program and waits for the user to input data
  164.      which will be placed in a_number and does NOT produce a movement to
  165.      a new line.
  166.  
  167.      readln(a_number);
  168.  
  169.      This command does exactly as read, but produces a movement to a new
  170.      line.
  171.  
  172.      write(a_number);
  173.  
  174.      This command writes the contents of a_number to the screen without a
  175.      new line.
  176.  
  177.      writeln(a_number);
  178.  
  179.      This command does exactly as write, but produces a movement to a new
  180.      line on the screen.
  181.  
  182.    These commands can be used with any combination of literal, variable,
  183.    or arithmetic expression. A literal is a defined statement.
  184.    3 is a literal in write(3);.  write(3); will write a 3 on the monitor.
  185.  
  186.    program tutorial3;
  187.      var
  188.        some_text: string;
  189.      begin
  190.        write('Type some text and press ENTER when done: ');
  191.        readln(some_text);
  192.        writeln('You just typed the following: ', some_text);
  193.      end.
  194.  
  195.    Proper events in this program will be (as it will appear on the screen):
  196.  
  197.  
  198.    Type some text and press ENTER when done: <input text here>
  199.    You just typed the following: <text here>
  200.  
  201.  
  202.    The readln prompts you to enter text which is rewritten with the last
  203.    writeln command.  If you type the examples in and compile them up to this
  204.    point (as I recommend -- it will help you learn), you will see exactly
  205.    how tutorial3 is supposed to work.
  206.  
  207. The End of Part 1
  208. =================
  209.         All of my tutorial parts will be formatted much like this one.  First
  210. I will cover some new topics, giving examples, then as the final act will
  211. always be a programming problem, which I will leave you, the reader, to
  212. solve, learning on your own.  The best thing to learn and get competent in
  213. any language is to actually sit down and program.  Any of the programming
  214. problems I leave you will not involve concepts that I have not covered in
  215. previous text, though I will try my best to make them challenging to further
  216. the reader's programming ability.  A solution to each of the problems  in each
  217. part will be presented in the next part.  My rules to you.  These are for
  218. your benefit.
  219.  
  220. 1) Do not ask anyone else to help you in any programming sub, or anywhere
  221.    in programming these little practice programs I give. You will not
  222.    learn anything, if at all, and your time looking through this will be
  223.    wasted.
  224. 2) Anyone who does know, please do not help anyone who is going through
  225.    this tutorial.  They will not learn if someone else gives them the answer!
  226. 3) Try and at least attempt the practice programming problem.  Do not just
  227.    sit and wait until I present a solution.  The syntax is easy to learn
  228.    from having notes and such, but the logic of actually programming some-
  229.    thing is only gotten by practice.
  230.  
  231. If you wish me to give your code from the practice programming problems in
  232. this tutorial a quick look, send 'em to me at ggrotz@2sprint.net.
  233.  
  234. Practice Programming Problem for Tutorial Part 1
  235. ================================================
  236.         Write a Pascal program (and entirely Pascal) which will accept
  237. two integers from the keyboard, presenting the user with a prompt to
  238. enter a number for each integer.  Then print out statements which tell us
  239. what the two digits add up to, subtract to, and multiply to.
  240. To be correct, you must act on the first number and then the second
  241. number in your computations.  For example, 14 and 7 (in that order) would
  242. be treated as 14+7, 14-7, and 14*7.
  243.  
  244.   Example Monitor Screen (using 14 and 7):
  245. ------------------------------------------------------------------------
  246.   Please enter the 1st number: 14
  247.   Please enter the 2nd number: 7
  248.  
  249.   Adding 14 and 7 gives 21.
  250.   Subtracting 7 from 14 gives 7.
  251.   Multiplying 14 and 7 gives 98.
  252.  
  253. Good luck!  And a solution to this problem will appear in part two!
  254.  
  255. Next Time
  256. =========
  257. Next time, we will discuss the use of decision-making (IF statements) in
  258. Pascal programming, as well as loops which repeat a defined, set number of
  259. times (FOR loops).  Eventually, when the tutorial is over, we will have
  260. covered most, if not all of the data and control structures, and a few
  261. special topics of interest to you (in Pascal).  If you have any comments
  262. please send them to ggrotz@2sprint.net.
  263.